package com.gaohe.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaohe.common.Result;
import com.gaohe.common.ResultMessage;
import com.gaohe.pojo.Admin;
import com.gaohe.pojo.Tag;
import com.gaohe.service.TagService;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/tags")
public class TagController {
    @Autowired
    private TagService tagService;

    /**
     * 分页 + 条件查询
     *
     * @param page
     * @param pageSize
     * @param name
     * @param type
     * @return
     */
    @GetMapping("/page")
    public Result page(
            @RequestParam int page,
            @RequestParam int pageSize,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String type
    ) {
        //分页
        Page<Tag> tagPage = new Page<>(page, pageSize);
        //构建条件
        LambdaQueryWrapper<Tag> qw = new LambdaQueryWrapper<>();
        //断言  ---  判空
        //1.null   2.”“  " "
        qw.like(Objects.nonNull(name) && !name.isBlank(), Tag::getName, name);
        qw.eq(Objects.nonNull(type) && !type.isBlank(), Tag::getType, type);
        //排序
        qw.orderByDesc(Tag::getUpdateTime);
        tagService.page(tagPage, qw);
        return Result.success(tagPage);
    }

    /**
     * 获取标签类型列表
     *
     * @return
     */
    @GetMapping("/getTagTypeList")
    public Result listTagType() {
        //方案1
//        LambdaQueryWrapper<Tag> qw = new LambdaQueryWrapper<>();
//        QueryWrapper
        //方案2：mybatis  手写sql
        return tagService.listTagType();
    }

    /**
     * 新增
     *
     * @param tag
     * @return
     */
    @PostMapping
    public Result save(@RequestBody Tag tag) {
        //方案一： 方案二：数据库默认值 方案三 isDeleted字段改为int类型
//        tag.setIsDeleted(0);
        //方案一: 手动封装 方案二：公共字段填充
        //获取当前登录信息
//        Admin loginAdmin = (Admin) session.getAttribute("loginAdmin");
//        tag.setCreateBy(loginAdmin.getUsername());
//        tag.setUpdateBy(loginAdmin.getUsername());
//        tag.setCreateTime(LocalDateTime.now());
//        tag.setUpdateTime(LocalDateTime.now());
        //唯一索引问题
        //1.try-catch
        //2.全局异常处理器
        boolean res = tagService.save(tag);
        return res ? Result.success(ResultMessage.SAVE_SUCCESS) : Result.error(ResultMessage.SAVE_ERROR);
    }

    /**
     * 根据 id 获取 tag标签对象
     *
     * @param id tagId
     * @return
     */
    @GetMapping("/{id}")
    public Result getTagById(@PathVariable Long id) {
        /*
            jackson库  --  java -->json
            objectMapper
            消息转换器

         */
        log.info("/tags:getTagById,参数为：{}", id);
        Tag tag = tagService.getById(id);
        return Result.success(tag);
    }

    /**
     * 根据id修改
     *
     * @param tag 实体类对象
     * @return
     */
    @PutMapping
    public Result modifyTagById(@RequestBody Tag tag) {
        boolean res = tagService.updateById(tag);
        return res ? Result.success(ResultMessage.MODIFY_SUCCESS)
                : Result.error(ResultMessage.MODIFY_ERROR);
    }

    /**
     * 根据 id 删除tag标签对象
     * @param id tagId
     * @return
     */
    @DeleteMapping
    public Result deleteTagById(@RequestParam("id") Long id){
        boolean res = tagService.removeById(id);
        return  res ? Result.success(ResultMessage.DELETE_SUCCESS)
                : Result.error(ResultMessage.DELETE_ERROR);
    }

    @GetMapping("/getTagNameListByType")
    public Result listTagNameByType(@RequestParam("type")String type){
        return tagService.listTagNameByType(type);
    }
}
